package com.codeoss.framework.view.util;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.codeoss.framework.context.YunContext;
import com.codeoss.framework.data.metadata.Order;
import com.codeoss.framework.data.metadata.Query;
import com.codeoss.framework.data.metadata.Restrictions;
import com.codeoss.framework.expression.Expression;
import com.codeoss.framework.expression.IScriptServiceStore;
import com.codeoss.framework.util.StringUtils;
import com.codeoss.framework.util.Utils;
import com.codeoss.framework.view.component.Page;
import com.codeoss.framework.view.datasource.DataSourceConfig;
import com.codeoss.framework.view.model.HierarchyData;

@Component
public class DataSourceUtil {

	@Autowired
	private IScriptServiceStore scriptServiceStore;
	@Autowired
	private Utils utils;
	public DataSourceConfig getDataSourceConfigById(Page page, String dataSourceId) {
		List<DataSourceConfig> dataSourceConfigList = page.getDataSourceConfigList();
		for (int i = 0; i < dataSourceConfigList.size(); i++) {
			DataSourceConfig dataSourceConfig = dataSourceConfigList.get(i);
			if (dataSourceConfig.getId().equals(dataSourceId)) {
				return dataSourceConfig;
			}
		}
		return null;
	}

	public HierarchyData convertTableDataToHierarchyData(YunContext yunContext, List<Map<String, Object>> dataList) {
		if (dataList != null && dataList.size() > 0) {
			Map<String, HierarchyData> hierarchyDataMap = new HashMap<String, HierarchyData>();
			for (int i = 0; i < dataList.size(); i++) {
				Map<String, Object> dataMap = dataList.get(i);
				HierarchyData hierarchyData = new HierarchyData();
				Object id = dataMap.get("id");
				String name = (String) dataMap.get("name");
				String icon = (String) dataMap.get("icon");
				String sId = String.valueOf(id);
				hierarchyData.setId(sId);
				hierarchyData.setName(name);
				hierarchyData.setIcon(icon);
				hierarchyDataMap.put(sId, hierarchyData);
			}
			HierarchyData root = null;
			for (int i = 0; i < dataList.size(); i++) {
				Map<String, Object> dataMap = dataList.get(i);
				Object id = dataMap.get("id");
				String sId = String.valueOf(id);
				HierarchyData data = hierarchyDataMap.get(sId);
				Object parentId = dataMap.get("parent_id");
				if (parentId == null) {
					root = data;
				} else {
					String sParentId = String.valueOf(parentId);
					HierarchyData parentData = hierarchyDataMap.get(sParentId);
					if (parentData == null) {
						root = data;
					} else {
						parentData.addChild(data);
					}
				}
			}
			root.setOpen(true);
			return root;
		}
		return null;
	}

	public void fillQuery(YunContext yunContext,Query query, DataSourceConfig dataSourceConfig) {
		Expression expression = dataSourceConfig.getWhereClause();
		if (expression != null) {
			String returnValue = (String) scriptServiceStore.getScriptService(expression).eval(expression.getScript(),
					yunContext.getVarValueMap());
			if (!StringUtils.isEmpty(returnValue)) {
				query.add(Restrictions.sql(returnValue));
			}
		}
		if(!utils.getHasOrderByClause(yunContext)) {
			String orderBy = dataSourceConfig.getOrderBy();
			if (!StringUtils.isEmpty(orderBy)) {
				query.addOrder(Order.sql(orderBy));
			}
		}
	}
}
